/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.crm.slcm.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import merlion.common.entity.SecurityRole;
import merlion.common.entity.Staff;
import merlion.common.util.Consts;
import merlion.crm.sopm.entity.RequestForQuotation;
import merlion.crm.sopm.entity.SalesInquiry;
import merlion.crm.sopm.entity.SalesQuotation;

/**
 *
 * @author Kenn
 */
@Entity
@NamedQuery(name = "getAllSalesLead", query = "SELECT sl from SalesLead sl WHERE TYPE(sl)=SalesLead")
public class SalesLead implements Serializable {

    private static final long serialVersionUID = 1L;
    // Attributes
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String clientId;
    private String companyName;
    private String category;
    private String industry;
    @OneToMany(targetEntity = ContactRecord.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    private Collection<ContactRecord> contactRecords;
    @OneToMany(targetEntity = Address.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    private Collection<Address> shippingAddress;
    @ManyToMany(targetEntity = Staff.class, fetch = FetchType.EAGER)
    private Collection<Staff> staffs;
    @OneToMany(targetEntity = SalesInquiry.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER, mappedBy = "salesLead")
    private Collection<SalesInquiry> salesInquiries;
    @OneToMany(targetEntity = RequestForQuotation.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER, mappedBy = "customer")
    private Collection<RequestForQuotation> requestForQuotations;
    @OneToMany(targetEntity = SalesQuotation.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER, mappedBy = "client")
    private Collection<SalesQuotation> salesQuotations;

    //--------------------------------------------------------------------------
    //                      Constructor and Create
    //--------------------------------------------------------------------------
    public SalesLead() {
        clientId = "";
        companyName = "";
        category = "";
        industry = "";
        contactRecords = new ArrayList<ContactRecord>();
        shippingAddress = new ArrayList<Address>();
        staffs = new ArrayList<Staff>();
        salesInquiries = new ArrayList<SalesInquiry>();
        requestForQuotations = new ArrayList<RequestForQuotation>();
        salesQuotations = new ArrayList<SalesQuotation>();
    }

    public void create(String clientId, String companyName, String category,
            Collection<ContactRecord> contactRecords, Staff ce, String industry) {
        this.setClientId(clientId);
        this.setCompanyName(companyName);
        this.setCategory(category);
        this.setContactRecords(new ArrayList<ContactRecord>());
        for (ContactRecord r : contactRecords) {
            this.contactRecords.add(r);
        }
        staffs = new ArrayList<Staff>();
        staffs.add(ce);
        salesInquiries = new ArrayList<SalesInquiry>();
        requestForQuotations = new ArrayList<RequestForQuotation>();
        salesQuotations = new ArrayList<SalesQuotation>();
        this.industry = industry;
    }

    public void addContactRecord(ContactRecord cr) {
        contactRecords.add(cr);
    }

    //-------------------------------------------------------
    //                  ACCESSORS / MUTATORS
    //-------------------------------------------------------
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Collection<ContactRecord> getContactRecords() {
        return contactRecords;
    }

    public void setContactRecords(Collection<ContactRecord> contactRecords) {
        this.contactRecords = contactRecords;
    }

    public Collection<Address> getShippingAddress() {
        return shippingAddress;
    }

    public void setShippingAddress(Collection<Address> shippingAddress) {
        this.shippingAddress = shippingAddress;
    }

    public Collection<Staff> getStaffs() {
        return staffs;
    }

    public void setStaffs(Collection<Staff> staffs) {
        this.staffs = staffs;
    }

    public Collection<RequestForQuotation> getRequestForQuotations() {
        return requestForQuotations;
    }

    public void setRequestForQuotations(Collection<RequestForQuotation> requestForQuotations) {
        this.requestForQuotations = requestForQuotations;
    }

    public Collection<SalesInquiry> getSalesInquiries() {
        return salesInquiries;
    }

    public void setSalesInquiries(Collection<SalesInquiry> salesInquiries) {
        this.salesInquiries = salesInquiries;
    }

    public Collection<SalesQuotation> getSalesQuotations() {
        return salesQuotations;
    }

    public void setSalesQuotations(Collection<SalesQuotation> salesQuotations) {
        this.salesQuotations = salesQuotations;
    }

    public String getIndustry() {
        return industry;
    }

    public void setIndustry(String industry) {
        this.industry = industry;
    }

    public Staff getCustomerExecutive() {

        Staff ce = null;
        for (Staff s : getStaffs()) {
            for (SecurityRole r : s.getSecurityRoles()) {
                if (r.getRoleName().equals(Consts.SALES_EXECUTIVE_ROLE)
                        || r.getRoleName().equals(Consts.SALES_VP_ROLE)
                        || r.getRoleName().equals(Consts.SALES_MANAGER_ROLE)) {
                    ce = s;
                }
            }
        }
        return ce;
    }

    public void setCustomerExecutive(Staff ce) {
        Staff oldCE = getCustomerExecutive();
        if (ce != null) {
            getStaffs().remove(oldCE);
            getStaffs().add(ce);
        }
    }

    public Staff getFinanceStaff() {

        Staff fs = null;
        for (Staff s : getStaffs()) {
            for (SecurityRole r : s.getSecurityRoles()) {
                if (r.getRoleName().equals(Consts.FIN_VP_ROLE)
                        ||r.getRoleName().equals(Consts.FIN_AP_STAFF_ROLE)
                        ||r.getRoleName().equals(Consts.FIN_STAFF_ROLE)) {
                    fs = s;
                }
            }
        }
        return fs;
    }

    public void setFinanceStaff(Staff fs) {
        Staff oldFS = getFinanceStaff();
        if (fs != null) {
            getStaffs().remove(oldFS);
            getStaffs().add(fs);
        }
    }

    //--------------------------------------------------------------------
    //                               Others
    //--------------------------------------------------------------------
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof SalesLead)) {
            return false;
        }
        SalesLead other = (SalesLead) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.crm.slcm.entity.SalesLead[ id=" + id + " ]";
    }
}
